<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="name">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="hAdd(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 添加弹层 -->
    <el-dialog
      width="30%"
      :title="isEdit?'编辑权限':'新增权限'"
      :visible.sync="showDialog"
      @close="resetForm"
    >
      <el-form ref="form" :model="formData" label-width="100px" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model.trim="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model.trim="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用" prop="enVisible">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            inactive-text="不启用"
          />
        </el-form-item>
      </el-form>
      <div style="text-align: right;">
        <el-button @click="showDialog=false">取消</el-button>
        <el-button type="primary" @click="hSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, getPermissionDetail, updatePermission, addPermission, delPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/tranListToTreeData'
export default {
  name: 'Permission',
  data() {
    return {
      isEdit: false,
      list: [],
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [
          { message: '不能为空', required: true, trigger: 'blur' }
        ],
        code: [
          { message: '不能为空', required: true, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      console.log('从后端获取的数据的是', res)
      // 转换成树形结构
      this.list = tranListToTreeData(res.data)
    },
    async hEdit(id) {
      try {
        const res = await getPermissionDetail(id)
        this.formData = res.data
        this.showDialog = true
        this.isEdit = true
      } catch (error) {
        console.log(error)
      }
    },
    async doEdit() {
      try {
        await updatePermission(this.formData)
        this.showDialog = false
        this.isEdit = false
        this.loadPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    hAdd(type, id) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = id
    },
    async doAdd() {
      try {
        await addPermission(this.formData)
        this.showDialog = false
        this.loadPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    resetForm() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      this.$refs.form.resetFields()
    },
    hSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        } else {
          console.log('提交失败')
        }
      })
    },
    // 删除
    hDel(id) {
      this.deleteByid(id)
      this.loadPermissionList()
    },
    async deleteByid(id) {
      try {
        await delPermission(id)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
